package dynamicProxy;

import java.lang.reflect.Proxy;

public class Demo {
    public static void main(String[] args) {
        // 下面两种测试方法都
//        dynamicDemo1();
        // dynamicDemo2();

        // 测试多层代理
        multiProxy();
    }

    public static void dynamicDemo1(){
        StockServiceImpl stockService = new StockServiceImpl();
        Class<?> clazz = stockService.getClass();
        DynamicProxy proxy = new DynamicProxy(stockService);

        StockService ss = (StockService) Proxy.newProxyInstance(clazz.getClassLoader(),clazz.getInterfaces(),proxy);
        ss.accessStock();
    }

    public static void dynamicDemo2(){
        DynamicProxy proxy = new DynamicProxy(new StockServiceImpl());
        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles","true");
        StockService ss = (StockService)(Proxy.newProxyInstance(StockService.class.getClassLoader(),new Class[]{StockService.class},proxy));
        ss.accessStock();
    }

    public static void multiProxy(){
        DynamicProxy proxy = new DynamicProxy(new StockServiceImpl());
        System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles","true");
        StockService ss = (StockService)(Proxy.newProxyInstance(StockService.class.getClassLoader(),new Class[]{StockService.class},proxy));

        DynamicProxyLog proxyLog = new DynamicProxyLog(ss);
        StockService ss1 = (StockService)(Proxy.newProxyInstance(StockService.class.getClassLoader(),new Class[]{StockService.class},proxyLog));
        ss1.accessStock();
    }
}
